package com.woniuxy.lrz.carlproject.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.common.util.BeanCopyUtil;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsCategoryAddOrUpdateForm;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsCategoryFindForm;
import com.woniuxy.lrz.carlproject.dao.GoodPropertyMapper;
import com.woniuxy.lrz.carlproject.dao.GoodPropertyValueMapper;
import com.woniuxy.lrz.carlproject.model.GoodCategory;
import com.woniuxy.lrz.carlproject.dao.GoodCategoryMapper;
import com.woniuxy.lrz.carlproject.model.GoodProperty;
import com.woniuxy.lrz.carlproject.model.GoodPropertyValue;
import com.woniuxy.lrz.carlproject.service.GoodCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.lrz.carlproject.service.dto.dtogoods.GoodsCategoryDto;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品分类表 服务实现类
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@Service
public class GoodCategoryServiceImpl extends ServiceImpl<GoodCategoryMapper, GoodCategory> implements GoodCategoryService {
    @Resource
    private GoodCategoryMapper goodCategoryMapper;
    @Resource
    private GoodPropertyMapper goodPropertyMapper;
    @Resource
    private GoodPropertyValueMapper goodPropertyValueMapper;
    @Override
    public boolean addGoodCategory(GoodsCategoryAddOrUpdateForm goodsCategoryAddOrUpdateForms) {
            //判断是新增还是修改
            GoodCategory good_category_name = goodCategoryMapper.selectOne(new QueryWrapper<GoodCategory>().eq("good_category_name", goodsCategoryAddOrUpdateForms.getGoodCategoryName()));
            GoodCategory goodCategory = BeanUtil.copyProperties(goodsCategoryAddOrUpdateForms, GoodCategory.class);
            if(goodsCategoryAddOrUpdateForms.getGoodCategoryId()==null){
                //判断新增的商品分类名称是否重名
                if(good_category_name!=null){
                    throw new AppException("有重名分类名，请稍后再试！");
                }
                goodCategoryMapper.insert(goodCategory);
            }
            //修改
            //判断修改后的名称有没有重名
            if(good_category_name!=null&&good_category_name.getGoodCategoryId()!=goodCategory.getGoodCategoryId()){
                throw new AppException("有重名分类名，请稍后再试！");
            }
            goodCategoryMapper.updateById(goodCategory);
        return true;
    }

    @Override
    public boolean deleteGoodCategory(Integer goodCategoryId) {
        //验证是否重复删除
        GoodCategory goodCategory = goodCategoryMapper.selectById(goodCategoryId);
        if(goodCategory==null){
            throw new AppException("请勿重复操作！");
        }
        //删除
        goodCategoryMapper.deleteById(goodCategoryId);
        //删除属性名和属性值
        List<GoodProperty> good_category_id = goodPropertyMapper.selectList(new QueryWrapper<GoodProperty>().eq("good_category_id", goodCategoryId));
        for (GoodProperty goodProperty : good_category_id) {
            goodPropertyValueMapper.delete(new QueryWrapper<GoodPropertyValue>().eq("property_id",goodProperty.getPropertyId()));
        }
        goodPropertyMapper.delete(new QueryWrapper<GoodProperty>().eq("good_category_id",goodCategoryId));

        return true;
    }

    @Override
    public Page<GoodsCategoryDto> findGoodCateGory(GoodsCategoryFindForm goodsCategoryFindForm) {
        Page<GoodCategory> page=new Page<>(goodsCategoryFindForm.getPageNum(),goodsCategoryFindForm.getPageSize());
        if(goodsCategoryFindForm.getGoodCategory()!=null){
            Page<GoodCategory> good_category_name = goodCategoryMapper.selectPage(page, new QueryWrapper<GoodCategory>().like("good_category_name", goodsCategoryFindForm.getGoodCategory()));
            Page<GoodsCategoryDto> goodsCategoryDtoPage = BeanCopyUtil.copyPageInfo(good_category_name, GoodsCategoryDto.class);
            return goodsCategoryDtoPage;
        }
        Page<GoodCategory> goodCategoryPage = goodCategoryMapper.selectPage(page, new QueryWrapper<>());
        Page<GoodsCategoryDto> goodsCategoryDtoPage = BeanCopyUtil.copyPageInfo(goodCategoryPage, GoodsCategoryDto.class);
        return goodsCategoryDtoPage;
    }
}
